package cn.iocoder.yudao.module.reading.dal.mysql.recording;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.reading.dal.dataobject.recording.UserRecordingDO;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

/**
 * 用户配音记录 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface UserRecordingMapper extends BaseMapperX<UserRecordingDO> {

    /**
     * 根据用户ID和绘本页面ID查询配音记录
     *
     * @param userId 用户ID
     * @param pageId 绘本页面ID
     * @return 配音记录
     */
    default UserRecordingDO selectByUserIdAndPageId(Long userId, Long pageId) {
        return selectOne(new LambdaQueryWrapperX<UserRecordingDO>()
                .eq(UserRecordingDO::getUserId, userId)
                .eq(UserRecordingDO::getPageId, pageId)
                .orderByDesc(UserRecordingDO::getCreateTime)
                .last("LIMIT 1"));
    }

    /**
     * 根据用户ID查询配音记录列表
     *
     * @param userId 用户ID
     * @param limit  限制数量
     * @return 配音记录列表
     */
    default List<UserRecordingDO> selectByUserId(Long userId, Integer limit) {
        return selectList(new LambdaQueryWrapperX<UserRecordingDO>()
                .eq(UserRecordingDO::getUserId, userId)
                .orderByDesc(UserRecordingDO::getCreateTime)
                .last(limit != null ? "LIMIT " + limit : ""));
    }

    /**
     * 根据绘本ID查询配音记录列表
     *
     * @param userId 用户ID
     * @param bookId 绘本ID
     * @return 配音记录列表
     */
    default List<UserRecordingDO> selectByUserIdAndBookId(Long userId, Long bookId) {
        return selectList(new LambdaQueryWrapperX<UserRecordingDO>()
                .eq(UserRecordingDO::getUserId, userId)
                .eq(UserRecordingDO::getBookId, bookId)
                .orderByAsc(UserRecordingDO::getPageId));
    }

    /**
     * 查询配音记录列表
     *
     * @param bookId 绘本ID（可选）
     * @param limit  限制数量
     * @return 配音记录列表
     */
    default List<UserRecordingDO> selectRecordings(Long bookId, Integer limit) {
        return selectList(new LambdaQueryWrapperX<UserRecordingDO>()
                .eqIfPresent(UserRecordingDO::getBookId, bookId)
                .orderByDesc(UserRecordingDO::getCreateTime)
                .last(limit != null ? "LIMIT " + limit : ""));
    }

    /**
     * 统计用户配音数量
     *
     * @param userId 用户ID
     * @return 配音数量
     */
    default Long countByUserId(Long userId) {
        return selectCount(new LambdaQueryWrapperX<UserRecordingDO>()
                .eq(UserRecordingDO::getUserId, userId));
    }

    /**
     * 软删除配音记录
     *
     * @param userId 用户ID
     * @param id     配音记录ID
     * @return 删除数量
     */
    default int softDeleteByUserIdAndId(Long userId, Long id) {
        // 使用 BaseMapperX 的软删除功能，它会自动设置 deleted = 1
        return delete(new LambdaQueryWrapperX<UserRecordingDO>()
                .eq(UserRecordingDO::getId, id)
                .eq(UserRecordingDO::getUserId, userId));
    }

    /**
     * 查询用户配音记录按绘本聚合的统计信息
     *
     * @param userId 用户ID
     * @return 按绘本聚合的配音记录列表
     */
    default List<UserRecordingDO> selectAggregateByUserId(Long userId) {
        return selectList(new LambdaQueryWrapperX<UserRecordingDO>()
                .eq(UserRecordingDO::getUserId, userId)
                .orderByDesc(UserRecordingDO::getCreateTime));
    }

    /**
     * 批量更新指定绘本的配音记录公开状态
     *
     * @param userId   用户ID
     * @param bookId   绘本ID
     * @param isPublic 是否公开
     * @return 更新数量
     */
    default int updateBookRecordingsPublicStatus(Long userId, Long bookId, Boolean isPublic) {
        UserRecordingDO updateObj = new UserRecordingDO();
        updateObj.setIsShareable(isPublic);

        return update(updateObj, new LambdaQueryWrapperX<UserRecordingDO>()
                .eq(UserRecordingDO::getUserId, userId)
                .eq(UserRecordingDO::getBookId, bookId));
    }

}
